﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using ByteSite.Common;
using System.Linq.Expressions;

namespace ByteSite.BLL
{
    /// <summary>
    /// 系统频道表
    /// </summary>
    public partial class forms
    {
        private readonly Entitys.sysconfig sysConfig = new BLL.sysconfig().loadConfig();//获得系统配置信息
        private readonly DAL.Data data;

        public forms()
        {
            data = new DAL.Data();
        }

        #region 基本方法================================
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            return data.Exists<Entitys.dt_forms>(id);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Entitys.dt_forms model)
        {
            try
            {
                data.BeginTran();

                model.id = data.Insert<Entitys.dt_forms>(model);
                List<int> fieldIds = new List<int>();//存储已加截的扩展字段ID集合
                List<int> showFieldIds = new List<int>();//存储已加截的扩展字段ID集合
                if (model.form_fields != null)
                {
                    //新增扩展字段及创建频道数据表
                    
                    foreach (Entitys.dt_form_field modelt in model.form_fields)
                    {
                        fieldIds.Add(Utils.ObjToInt(modelt.field_id));
                        data.Insert<Entitys.dt_form_field>(new Entitys.dt_form_field() { form_id = model.id, field_id = modelt.field_id });
                    }
                }
                if (model.show_form_fields != null)
                {
                    foreach (Entitys.dt_form_show_column modelt in model.show_form_fields)
                    {
                        showFieldIds.Add(Utils.ObjToInt(modelt.field_id));
                        Entitys.dt_form_attribute_field field = data.Get<Entitys.dt_form_attribute_field>(Utils.ObjToInt(modelt.field_id));
                        data.Insert<Entitys.dt_form_show_column>(new Entitys.dt_form_show_column() { form_id = model.id, field_id = modelt.field_id, field_name = field.name, sort_id = 99, is_lock = 0, delete_flag = 0, add_time = DateTime.Now, update_time = DateTime.Now, add_user = model.add_user, update_user = model.add_user });
                    }
                }
                //创建频道数据表
                string table = sysConfig.sysdatabaseprefix + DTKeys.TABLE_FORM_DATA + model.name;
                 data.CreateTable(table, typeof(Entitys.form));



                #region 写入导航数据===============
                int siteNavParentId = new DAL.navigation().GetNavId("form_data_manage");
                new DAL.navigation().Delete(x => x.name == "form_data_" + model.name);
                new DAL.navigation().Add(new Entitys.dt_navigation() { action_type = "Show,View,Add,Edit,Delete", channel_id = 0, icon_url = "", is_lock = 0, is_sys = 0, link_url = "form/data_list.aspx?form_id=" + model.id, name = "form_data_" + model.name, nav_type = "System", parent_id = siteNavParentId, remark = "", sort_id = 99, sub_title = "", title = model.title });

                //new DAL.navigation().Add(parent_id, "channel_" + model.name, model.title, "", Utils.ObjToInt(model.sort_id), model.id, "Show");
                //int newNavId = new DAL.navigation().Add(parent_id, "channel_" + model.name, model.title, "", Utils.ObjToInt(model.sort_id), model.id, "Show");
                //new DAL.navigation().Add( newNavId, "channel_" + model.name + "_list", "内容管理", "article/article_list.aspx", 99, model.id, "Show,View,Add,Edit,Delete,Audit");
                //new DAL.navigation().Add( newNavId, "channel_" + model.name + "_category", "栏目类别", "article/category_list.aspx", 100, model.id, "Show,View,Add,Edit,Delete");
                #endregion

                data.CommitTran();
                if (fieldIds.Count > 0)
                {
                    //查询扩展字段表
                    List<Entitys.dt_form_attribute_field> fields = data.List<Entitys.dt_form_attribute_field>(x => fieldIds.Contains(x.id), x => x.sort_id);

                    foreach (Entitys.dt_form_attribute_field field in fields)
                    {
                        SqlSugar.DbColumnInfo dbColumn = new SqlSugar.DbColumnInfo();
                        dbColumn.DbColumnName = field.name;
                        if (field.control_type == "single-text")
                        {
                            dbColumn.DataType = data.GetDbTypeName(typeof(System.String).Name);
                            dbColumn.PropertyType = typeof(System.String);
                            dbColumn.Length = Utils.ObjToInt(field.data_length);
                            dbColumn.ColumnDescription = field.name;
                            dbColumn.DefaultValue = field.default_value;
                            dbColumn.IsNullable = true;
                            data.AddColumn(table, dbColumn);
                        }
                        else if (field.control_type == "number" && field.data_place > 0)
                        {
                            dbColumn.DataType = data.GetDbTypeName(typeof(System.Decimal).Name);
                            dbColumn.PropertyType = typeof(System.Decimal);
                            dbColumn.Length = Utils.ObjToInt(9);
                            dbColumn.DecimalDigits = Utils.ObjToInt(field.data_place);
                            dbColumn.ColumnDescription = field.name;
                            dbColumn.DefaultValue = field.default_value;
                            dbColumn.IsNullable = true;
                            data.AddColumn(table, dbColumn);
                        }
                        else if (field.control_type == "number" && field.data_place == 0)
                        {
                            dbColumn.DataType = data.GetDbTypeName(typeof(System.Int32).Name);
                            dbColumn.PropertyType = typeof(System.Int32);
                            dbColumn.ColumnDescription = field.name;
                            dbColumn.DefaultValue = field.default_value;
                            dbColumn.IsNullable = true;
                            data.AddColumn(table, dbColumn);
                        }
                        else if (field.control_type == "datetime")
                        {
                            dbColumn.DataType = data.GetDbTypeName(typeof(System.DateTime).Name);
                            dbColumn.PropertyType = typeof(System.DateTime);
                            dbColumn.ColumnDescription = field.name;
                            dbColumn.DefaultValue = field.default_value;
                            dbColumn.IsNullable = true;
                            data.AddColumn(table, dbColumn);
                        }
                        else if (field.control_type == "multi-text")
                        {
                            dbColumn.DataType = data.GetDbTypeName(typeof(System.String).Name);
                            dbColumn.PropertyType = typeof(System.String);
                            dbColumn.Length = Utils.ObjToInt(field.data_length);
                            dbColumn.ColumnDescription = field.name;
                            dbColumn.DefaultValue = field.default_value;
                            dbColumn.IsNullable = true;
                            data.AddColumn(table, dbColumn);
                        }
                        else if (field.control_type == "editor")
                        {
                            dbColumn.DataType = data.GetDbTypeName(typeof(System.String).Name);
                            dbColumn.PropertyType = typeof(System.String);
                            dbColumn.Length = int.MaxValue;
                            dbColumn.ColumnDescription = field.name;
                            dbColumn.DefaultValue = field.default_value;
                            dbColumn.IsNullable = true;
                            data.AddColumn(table, dbColumn);
                        }
                        else if (field.control_type == "image" || field.control_type == "video")
                        {
                            dbColumn.DataType = data.GetDbTypeName(typeof(System.String).Name);
                            dbColumn.PropertyType = typeof(System.String);
                            dbColumn.Length = 255;
                            dbColumn.ColumnDescription = field.name;
                            dbColumn.DefaultValue = field.default_value;
                            dbColumn.IsNullable = true;
                            data.AddColumn(table, dbColumn);
                        }
                        else if (field.control_type == "multi-images" || field.control_type == "multi-video" || field.control_type == "multi-files")
                        {
                            string list_table = sysConfig.sysdatabaseprefix + DTKeys.TABLE_FORM_DATA_LIST;

                        }
                        else if (field.control_type == "checkbox")
                        {
                            dbColumn.DataType = data.GetDbTypeName(typeof(System.Int32).Name);
                            dbColumn.PropertyType = typeof(System.Int32);
                            dbColumn.ColumnDescription = field.name;
                            dbColumn.DefaultValue = field.default_value;
                            dbColumn.IsNullable = true;
                            data.AddColumn(table, dbColumn);
                        }
                        else if (field.control_type == "multi-radio" && field.data_type == "int")
                        {
                            dbColumn.DataType = data.GetDbTypeName(typeof(System.String).Name);
                            dbColumn.PropertyType = typeof(System.String);
                            dbColumn.ColumnDescription = field.name;
                            dbColumn.DefaultValue = field.default_value;
                            dbColumn.IsNullable = true;
                            data.AddColumn(table, dbColumn);
                        }
                        else if (field.control_type == "multi-radio" && field.data_type.StartsWith("nvarchar"))
                        {
                            dbColumn.DataType = data.GetDbTypeName(typeof(System.String).Name);
                            dbColumn.PropertyType = typeof(System.String);
                            dbColumn.Length = Utils.ObjToInt(field.data_length);
                            dbColumn.ColumnDescription = field.name;
                            dbColumn.DefaultValue = field.default_value;
                            dbColumn.IsNullable = true;
                            data.AddColumn(table, dbColumn);
                        }
                        else if (field.control_type == "multi-checkbox")
                        {
                            dbColumn.DataType = data.GetDbTypeName(typeof(System.String).Name);
                            dbColumn.PropertyType = typeof(System.String);
                            dbColumn.Length = Utils.ObjToInt(field.data_length);
                            dbColumn.ColumnDescription = field.name;
                            dbColumn.DefaultValue = field.default_value;
                            dbColumn.IsNullable = true;
                            data.AddColumn(table, dbColumn);
                        }
                        else if (field.control_type == "multi-dropdownlist")
                        {
                            dbColumn.DataType = data.GetDbTypeName(typeof(System.String).Name);
                            dbColumn.PropertyType = typeof(System.String);
                            dbColumn.Length = Utils.ObjToInt(field.data_length);
                            dbColumn.ColumnDescription = field.name;
                            dbColumn.DefaultValue = field.default_value;
                            dbColumn.IsNullable = true;
                            data.AddColumn(table, dbColumn);
                        }
                        else if (field.control_type == "dropdownlist")
                        {
                            dbColumn.DataType = data.GetDbTypeName(typeof(System.String).Name);
                            dbColumn.PropertyType = typeof(System.String);
                            dbColumn.Length = Utils.ObjToInt(field.data_length);
                            dbColumn.ColumnDescription = field.name;
                            dbColumn.DefaultValue = field.default_value;
                            dbColumn.IsNullable = true;
                            data.AddColumn(table, dbColumn);
                        }
                        else
                        {
                            dbColumn.DataType = data.GetDbTypeName(typeof(System.String).Name);
                            dbColumn.PropertyType = typeof(System.String);
                            dbColumn.Length = 255;
                            dbColumn.ColumnDescription = field.name;
                            dbColumn.DefaultValue = field.default_value;
                            dbColumn.IsNullable = true;
                            data.AddColumn(table, dbColumn);
                        }
                        //dbColumn.DecimalDigits
                    }
                }
            }
            catch (Exception ex)
            {
                data.RollbackTran();
                return 0;
            }
            return model.id;
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Entitys.dt_forms model)
        {
            int siteNavParentId = new DAL.navigation().GetNavId("form_data_manage");
            if (siteNavParentId == 0)
            {
                return false;
            }
            Entitys.dt_forms prevModel = GetModel(model.id);

            try
            {
                data.BeginTran();
                string table = sysConfig.sysdatabaseprefix + DTKeys.TABLE_FORM_DATA + prevModel.name;
                data.Update<Entitys.dt_forms>(model);

                //删除已移除的扩展字段

                if (prevModel.form_fields != null)
                {
                    List<int> fieldIds = new List<int>();
                    foreach (Entitys.dt_form_field modelt in prevModel.form_fields)
                    {
                        if (model.form_fields.Find(p => p.field_id == modelt.field_id) == null)
                        {
                            fieldIds.Add(Utils.ObjToInt(modelt.id));
                        }
                    }

                    if (fieldIds.Count > 0)
                    {
                        
                        List<Entitys.dt_form_field> fieldList = data.List<Entitys.dt_form_field>(x => fieldIds.Contains(x.id));
                        //List<Entitys.dt_form_field> fieldList = data.List<Entitys.dt_form_field>(x => fieldIds.Contains(Utils.ObjToInt(x.field_id)));
                        foreach (Entitys.dt_form_field f in fieldList)
                        {
                            Entitys.dt_form_attribute_field formAttributeField = data.Get<Entitys.dt_form_attribute_field>(x => x.id == f.field_id);
                            if (formAttributeField != null)
                            {
                                try
                                {
                                    data.DropColumn(table, formAttributeField.name);
                                }
                                catch
                                {

                                }
                                data.Delete<Entitys.dt_form_field>(x => x.form_id == model.id && x.field_id == f.field_id);


                            }
                        }
                    }

                }

                if (prevModel.show_form_fields != null)
                {
                    List<int> showFieldIds = new List<int>();
                    foreach (Entitys.dt_form_show_column modelt in prevModel.show_form_fields)
                    {
                        if (model.show_form_fields.Find(p => p.field_id == modelt.field_id) == null)
                        {
                            showFieldIds.Add(Utils.ObjToInt(modelt.id));
                        }
                    }
                    if (showFieldIds.Count > 0)
                    {
                        List<Entitys.dt_form_show_column> fieldList = data.List<Entitys.dt_form_show_column>(x => showFieldIds.Contains(x.id));
                        //List<Entitys.dt_form_field> fieldList = data.List<Entitys.dt_form_field>(x => fieldIds.Contains(Utils.ObjToInt(x.field_id)));
                        foreach (Entitys.dt_form_show_column f in fieldList)
                        {
                            data.Delete<Entitys.dt_form_show_column>(x => x.form_id == model.id && x.field_id == f.field_id);

                            //Entitys.dt_form_attribute_field formAttributeField = data.Get<Entitys.dt_form_attribute_field>(x => x.id == f.field_id);
                        }
                    }
                }

                if (model.form_fields != null)
                {
                    List<int> newFieldIds = new List<int>();
                    foreach (Entitys.dt_form_field modelt in model.form_fields)
                    {
                        Entitys.dt_form_field fieldModel = null;
                        if (prevModel.form_fields != null)
                        {
                            fieldModel = prevModel.form_fields.Find(p => p.field_id == modelt.field_id);
                        }
                        //如不存在字段则添加
                        if (fieldModel == null)
                        {
                            newFieldIds.Add(Utils.ObjToInt(modelt.field_id));
                            data.Insert<Entitys.dt_form_field>(new Entitys.dt_form_field() { form_id = model.id, field_id = modelt.field_id });
                        }
                    }
                    
                    if (newFieldIds.Count > 0)
                    {
                        List<Entitys.dt_form_attribute_field> list = data.List<Entitys.dt_form_attribute_field>(x => newFieldIds.Contains(x.id), x => x.sort_id);
                        foreach (Entitys.dt_form_attribute_field field in list)
                        {
                            SqlSugar.DbColumnInfo dbColumn = new SqlSugar.DbColumnInfo();
                            dbColumn.DbColumnName = field.name;
                            if (field.control_type == "single-text")
                            {
                                dbColumn.DataType = data.GetDbTypeName(typeof(System.String).Name);
                                dbColumn.PropertyType = typeof(System.String);
                                dbColumn.Length = Utils.ObjToInt(field.data_length);
                                dbColumn.ColumnDescription = field.name;
                                dbColumn.DefaultValue = field.default_value;
                                dbColumn.IsNullable = true;
                                data.AddColumn(table, dbColumn);
                            }
                            else if (field.control_type == "number" && field.data_place > 0)
                            {
                                dbColumn.DataType = data.GetDbTypeName(typeof(System.Decimal).Name);
                                dbColumn.PropertyType = typeof(System.Decimal);
                                dbColumn.Length = Utils.ObjToInt(9);
                                dbColumn.DecimalDigits = Utils.ObjToInt(field.data_place);
                                dbColumn.ColumnDescription = field.name;
                                dbColumn.DefaultValue = field.default_value;
                                dbColumn.IsNullable = true;
                                data.AddColumn(table, dbColumn);
                            }
                            else if (field.control_type == "number" && field.data_place == 0)
                            {
                                dbColumn.DataType = data.GetDbTypeName(typeof(System.Int32).Name);
                                dbColumn.PropertyType = typeof(System.Int32);
                                dbColumn.ColumnDescription = field.name;
                                dbColumn.DefaultValue = field.default_value;
                                dbColumn.IsNullable = true;
                                data.AddColumn(table, dbColumn);
                            }
                            else if (field.control_type == "datetime")
                            {
                                dbColumn.DataType = data.GetDbTypeName(typeof(System.DateTime).Name);
                                dbColumn.PropertyType = typeof(System.DateTime);
                                dbColumn.ColumnDescription = field.name;
                                dbColumn.DefaultValue = field.default_value;
                                dbColumn.IsNullable = true;
                                data.AddColumn(table, dbColumn);
                            }
                            else if (field.control_type == "multi-text")
                            {
                                dbColumn.DataType = data.GetDbTypeName(typeof(System.String).Name);
                                dbColumn.PropertyType = typeof(System.String);
                                dbColumn.Length = Utils.ObjToInt(field.data_length);
                                dbColumn.ColumnDescription = field.name;
                                dbColumn.DefaultValue = field.default_value;
                                dbColumn.IsNullable = true;
                                data.AddColumn(table, dbColumn);
                            }
                            else if (field.control_type == "editor")
                            {
                                dbColumn.DataType = data.GetDbTypeName(typeof(System.String).Name);
                                dbColumn.PropertyType = typeof(System.String);
                                dbColumn.Length = int.MaxValue;
                                dbColumn.ColumnDescription = field.name;
                                dbColumn.DefaultValue = field.default_value;
                                dbColumn.IsNullable = true;
                                data.AddColumn(table, dbColumn);
                            }
                            else if (field.control_type == "image" || field.control_type == "video")
                            {
                                dbColumn.DataType = data.GetDbTypeName(typeof(System.String).Name);
                                dbColumn.PropertyType = typeof(System.String);
                                dbColumn.Length = 255;
                                dbColumn.ColumnDescription = field.name;
                                dbColumn.DefaultValue = field.default_value;
                                dbColumn.IsNullable = true;
                                data.AddColumn(table, dbColumn);
                            }
                            else if (field.control_type == "multi-images" || field.control_type == "multi-video" || field.control_type == "multi-files")
                            {
                                string list_table = sysConfig.sysdatabaseprefix + DTKeys.TABLE_FORM_DATA_LIST;

                            }
                            else if (field.control_type == "checkbox")
                            {
                                dbColumn.DataType = data.GetDbTypeName(typeof(System.Int32).Name);
                                dbColumn.PropertyType = typeof(System.Int32);
                                dbColumn.ColumnDescription = field.name;
                                dbColumn.DefaultValue = field.default_value;
                                dbColumn.IsNullable = true;
                                data.AddColumn(table, dbColumn);
                            }
                            else if (field.control_type == "multi-radio" && field.data_type == "int")
                            {
                                dbColumn.DataType = data.GetDbTypeName(typeof(System.Int32).Name);
                                dbColumn.PropertyType = typeof(System.Int32);
                                dbColumn.ColumnDescription = field.name;
                                dbColumn.DefaultValue = field.default_value;
                                dbColumn.IsNullable = true;
                                data.AddColumn(table, dbColumn);
                            }
                            else if (field.control_type == "multi-radio" && field.data_type.StartsWith("nvarchar"))
                            {
                                dbColumn.DataType = data.GetDbTypeName(typeof(System.String).Name);
                                dbColumn.PropertyType = typeof(System.String);
                                dbColumn.Length = Utils.ObjToInt(field.data_length);
                                dbColumn.ColumnDescription = field.name;
                                dbColumn.DefaultValue = field.default_value;
                                dbColumn.IsNullable = true;
                                data.AddColumn(table, dbColumn);
                            }
                            else if (field.control_type == "multi-checkbox")
                            {
                                dbColumn.DataType = data.GetDbTypeName(typeof(System.String).Name);
                                dbColumn.PropertyType = typeof(System.String);
                                dbColumn.Length = Utils.ObjToInt(field.data_length);
                                dbColumn.ColumnDescription = field.name;
                                dbColumn.DefaultValue = field.default_value;
                                dbColumn.IsNullable = true;
                                data.AddColumn(table, dbColumn);
                            }
                            else if (field.control_type == "multi-dropdownlist")
                            {
                                dbColumn.DataType = data.GetDbTypeName(typeof(System.String).Name);
                                dbColumn.PropertyType = typeof(System.String);
                                dbColumn.Length = Utils.ObjToInt(field.data_length);
                                dbColumn.ColumnDescription = field.name;
                                dbColumn.DefaultValue = field.default_value;
                                dbColumn.IsNullable = true;
                                data.AddColumn(table, dbColumn);
                            }
                            else if (field.control_type == "dropdownlist")
                            {
                                dbColumn.DataType = data.GetDbTypeName(typeof(System.String).Name);
                                dbColumn.PropertyType = typeof(System.String);
                                dbColumn.Length = Utils.ObjToInt(field.data_length);
                                dbColumn.ColumnDescription = field.name;
                                dbColumn.DefaultValue = field.default_value;
                                dbColumn.IsNullable = true;
                                data.AddColumn(table, dbColumn);
                            }
                            else
                            {
                                dbColumn.DataType = data.GetDbTypeName(typeof(System.String).Name);
                                dbColumn.PropertyType = typeof(System.String);
                                dbColumn.Length = 255;
                                dbColumn.ColumnDescription = field.name;
                                dbColumn.DefaultValue = field.default_value;
                                dbColumn.IsNullable = true;
                                data.AddColumn(table, dbColumn);
                            }
                            //dbColumn.DecimalDigits
                        }
                    }
                }

                if (model.show_form_fields != null)
                {
                    List<int> newShowFieldIds = new List<int>();
                    foreach (Entitys.dt_form_show_column modelt in model.show_form_fields)
                    {
                        Entitys.dt_form_show_column fieldModel = null;
                        if (prevModel.show_form_fields != null)
                        {
                            fieldModel = prevModel.show_form_fields.Find(p => p.field_id == modelt.field_id);
                        }
                        //如不存在字段则添加
                        if (fieldModel == null)
                        {
                            newShowFieldIds.Add(Utils.ObjToInt(modelt.field_id));

                            Entitys.dt_form_attribute_field field = data.Get<Entitys.dt_form_attribute_field>(Utils.ObjToInt(modelt.field_id));
                            data.Insert<Entitys.dt_form_show_column>(new Entitys.dt_form_show_column() { form_id = model.id, field_id = modelt.field_id, field_name = field.name, sort_id = 99, is_lock = 0, delete_flag = 0, add_time = DateTime.Now, update_time = DateTime.Now, add_user = model.add_user, update_user = model.add_user });
                        }
                    }

                }

                //如果频道名称改变则需要更改数据表名
                if (model.name != prevModel.name)
                {
                    string newTable = sysConfig.sysdatabaseprefix + DTKeys.TABLE_FORM_DATA + model.name;
                    data.RenameTable(table, newTable);
                }

                #region 编辑对应的导航==================

                new DAL.navigation().Delete(x => x.name == "form_data_" + model.name);
                new DAL.navigation().Add(new Entitys.dt_navigation() { action_type = "Show,View,Add,Edit,Delete", channel_id = 0, icon_url = "", is_lock = 0, is_sys = 0, link_url = "form/data_list.aspx?form_id=" + model.id, name = "form_data_" + model.name, nav_type = "System", parent_id = siteNavParentId, remark = "", sort_id = 99, sub_title = "", title = model.title });

                //new DAL.navigation().Update("channel_" + prevModel.name, siteNavParentId, "channel_" + model.name, model.title, Utils.ObjToInt(model.sort_id));
                //new DAL.navigation().Update( "channel_" + prevModel.name + "_list", "channel_" + model.name + "_list"); //内容管理
                //new DAL.navigation().Update("channel_" + prevModel.name + "_category", "channel_" + model.name + "_category"); //栏目类别

               
                #endregion

                data.CommitTran();
            }
            catch(Exception ex)
            {
                data.RollbackTran();
                return false;
            }
            return true;

        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {
            //取得频道的名称
            string formName = GetFormName(id);
            if (string.IsNullOrEmpty(formName))
            {
                return false;
            }

            try
            {
                data.BeginTran();
                //取得要删除的所有导航ID
                //List<int> navIds = new DAL.navigation().GetIds(DTKeys.TABLE_FORM_DATA + formName);
                //List<int> navIdList = new List<int>();
                //foreach (int nid in navIds)
                //{
                //    navIdList.Add(nid);
                //}

                //删除导航菜单表
                //data.Delete<Entitys.dt_navigation>(x => navIdList.Contains(x.id));

                //删除扩展字段表
                data.Delete<Entitys.dt_form_field>(x => x.form_id == id);
                data.Delete<Entitys.dt_form_show_column>(x => x.form_id == id);

                //删除频道数据表
                data.DropTable(sysConfig.sysdatabaseprefix + DTKeys.TABLE_FORM_DATA + formName);

                //删除频道表
                data.Delete<Entitys.dt_forms>(x => x.id == id);

                data.CommitTran();
            }
            catch (Exception ex)
            {
                data.RollbackTran();
                return false;
            }
            return true;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Entitys.dt_forms GetModel(int id)
        {
            Entitys.dt_forms model = data.Get<Entitys.dt_forms>(id);

            List<Entitys.dt_form_field> fields = data.List<Entitys.dt_form_field>(x => x.form_id == model.id);
            if (fields != null)
            {
                if (fields.Count > 0)
                {
                    model.form_fields = new List<Entitys.dt_form_field>();
                    model.form_fields.AddRange(fields);
                }
            }

            List<Entitys.dt_form_show_column> show_fields = data.List<Entitys.dt_form_show_column>(x => x.form_id == model.id);
            if (show_fields != null)
            {
                if (show_fields.Count > 0)
                {
                    model.show_form_fields = new List<Entitys.dt_form_show_column>();
                    model.show_form_fields.AddRange(show_fields);
                }
            }
            return model;
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public List<Entitys.dt_forms> GetList(int top, string strWhere, string filedOrder)
        {
            return data.List<Entitys.dt_forms>(top, strWhere, filedOrder);
        }

        public List<Entitys.dt_forms> GetList(Expression<Func<Entitys.dt_forms, bool>> whereExpression, Expression<Func<Entitys.dt_forms, object>> orderExpression)
        {
            return data.List<Entitys.dt_forms>(whereExpression, orderExpression);
        }

        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public List<Entitys.dt_forms> GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            return data.List<Entitys.dt_forms>(pageSize, pageIndex, strWhere, filedOrder, out recordCount);
        }
        #endregion

        #region 扩展方法================================
        /// <summary>
        /// 查询频道名称是否存在
        /// </summary>
        public bool Exists(string name)
        {
            if (data.Exists<Entitys.dt_forms>(x=>x.name==name))
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Entitys.dt_forms GetModel(string form_name)
        {
            Entitys.dt_forms model = data.Get<Entitys.dt_forms>(x => x.name == form_name);

            List<Entitys.dt_form_field> fields = data.List<Entitys.dt_form_field>(x => x.form_id == model.id);
            if (fields != null)
            {
                if (fields.Count > 0)
                {
                    model.form_fields = new List<Entitys.dt_form_field>();
                    model.form_fields.AddRange(fields);
                }
            }

            List<Entitys.dt_form_show_column> show_fields = data.List<Entitys.dt_form_show_column>(x => x.form_id == model.id);
            if (show_fields != null)
            {
                if (show_fields.Count > 0)
                {
                    model.show_form_fields = new List<Entitys.dt_form_show_column>();
                    model.show_form_fields.AddRange(show_fields);
                }
            }
            return model;
        }

        /// <summary>
        /// 返回数据总数
        /// </summary>
        public int GetCount(Expression<Func<Entitys.dt_forms, bool>> whereExpression)
        {
            return data.Count<Entitys.dt_forms>(whereExpression);
        }


        /// <summary>
        /// 返回表单名称
        /// </summary>
        public string GetFormName(int id)
        {
            Entitys.dt_forms model = data.Get<Entitys.dt_forms>(id);
            if (model != null)
                return model.name;
            else
                return "";
        }

        public string GetFormTitle(int id)
        {
            Entitys.dt_forms model = data.Get<Entitys.dt_forms>(id);
            if (model != null)
                return model.title;
            else
                return "";
        }

        /// <summary>
        /// 保存排序
        /// </summary>
        public bool UpdateSort(int id, int sort_id)
        {
            //取得频道的名称
            string form_name = GetFormName(id);
            if (form_name == string.Empty)
            {
                return false;
            }
            return data.UpdateColumns<Entitys.dt_forms>(x => new Entitys.dt_forms { sort_id = sort_id }, x => x.id == id);
        }
        #endregion
    }
}